// JavaScript Document
var sy = {
	//认证
	real: function (data) {
        $.commonAjax({
            url: 'B043',
            type: 'POST',
            data: data,
			isShowError: true,
            success: function (res) {
				$.alert(res.msg);
                console.log(res);
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
	},personalCardImgA:'',
	personalCardImgB:'',
	personal:function(){
		$.commonAjax({
            url: 'B045',
            type: 'GET',
            data: {},
			isShowError: true,
            success: function (res) {
				console.log(res.data);
				$('input:eq(0)').val(res.data.realName);
				$('input:eq(1)').val(res.data.identityCode);
				$('.uploadImg:eq(0)').html('<img src="'+CONFIG.imgUrl+res.data.personalCardImgA+'" style="display:block;width:100%;">').show();
				$('.uploadImg:eq(1)').html('<img src="'+CONFIG.imgUrl+res.data.personalCardImgB+'" style="display:block;width:100%;">').show();
				if(res.data.status == 1){
					$('.subBtn').prop('disabled',true).val('已认证').css('background-color','gray');
				}
				 if(res.data.status != 0 && res.data.status != 1){
					$('.subBtn').prop('disabled',true).val('认证中').css('background-color','gray');
				 }
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
	}
}
$(function () {
	sy.personal();
	//H5上传
	$('input[id^="file_"]').change(function () {
		var num = $(this).prop('id').split('_')[1];
		var fd = new FormData();
		fd.append('fileType','personal');
		
		var reader = new FileReader();
        reader.readAsDataURL($(this)[0].files[0]);
		fd.append(0, $(this)[0].files[0]);
		$.ajax({
			url:CONFIG.apiUrl+ '/upload/documentByH5.api',
			type: 'post',
			data: fd,
			processData: false,
			contentType: false,
			beforeSend: function(){
				if(layer)
					layer.closeAll('loading');
				$.layerload();
			},
			success: function (res) {
				if(layer)
					layer.closeAll('loading');
				if(res.code && res.code!=200){
					$.layermsg(res.msg);
					return false;
				}
				var fileList = res.data;
				if (null == fileList || fileList.length == 0) {
					$.layermsg("文件上传失败");
					return false;
				}
				var urlZ =fileList[0].path;
				if(num==1){
					sy.personalCardImgA = urlZ;
					$('.uploadImg:first').show().html('<img src="'+CONFIG.imgUrl + urlZ+'" style="display:block;width:100%;">');
				}else{
					sy.personalCardImgB = urlZ;
					$('.uploadImg:last').show().html('<img src="'+CONFIG.imgUrl + urlZ+'" style="display:block;width:100%;">');
				}
				/*
				$("#"+_id).siblings("img").attr("src",CONFIG.imgUrl + urlZ);
				$("#"+_id).siblings("img").attr("filePath",urlZ);
				$("#"+_id).addClass("alphaClass");
				$("#"+_id).siblings("span").addClass("alphaClass"); */                       
			},
			error : function () {
				if(layer)
					layer.closeAll('loading');
				$.layermsg("图片上传异常，请重试");
			}
		})
             //提交
		$('.subBtn').click(function(){
			var realName = $('.info:first').val();	
			var identityCode = $('.info:last').val();
			var data = {realName:realName,identityCode:identityCode,personalCardImgA:sy.personalCardImgA,personalCardImgB:sy.personalCardImgB};
			sy.real(data);
		});   
	});
})